package selcoursesys.service.impl;

import org.apache.struts2.ServletActionContext;
import selcoursesys.action.elevInfo;
import selcoursesys.action.getAcadeByStuid;
import selcoursesys.dao.CourseDAO;
import selcoursesys.dao.SelectionDAO;
import selcoursesys.dao.impl.CourseDAOImpo;
import selcoursesys.dao.impl.SelectionDAOImpl;
import selcoursesys.entity.CourseEntity;
import selcoursesys.entity.SelectionEntity;

import java.util.Iterator;
import java.util.List;

/**
 * Created by Lpbdz on 2017/4/13.
 */
public class numCheck {
    public static boolean checkNum(String courseid){
        CourseDAO courseDAO=new CourseDAOImpo();
        CourseEntity courseEntity=courseDAO.queryCourseById(courseid);
        if(courseEntity.getLnum()==courseEntity.getCnum()){
            return false;
        }
        return true;
    }
    public static boolean checkTime(String stuid,String courseid) {
        //GET 所有上课时间
        CourseDAO currcourseDAO = new CourseDAOImpo();
        CourseEntity courseEntity = currcourseDAO.queryCourseById(courseid);//当前课程

        SelectionEntity selectionEntity = new SelectionEntity();
        selectionEntity.setStuid(stuid);
        SelectionDAO selectionDAO = new SelectionDAOImpl();
        List list = selectionDAO.querySelection(selectionEntity);
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            SelectionEntity selectionEntity1 = (SelectionEntity) iterator.next();
            CourseDAO courseDAO = new CourseDAOImpo();
            CourseEntity courseEntity1 = courseDAO.queryCourseById(selectionEntity1.getCourseid());
            //开始周>结束周,结束周<开始周,不存在选课时间冲突
            if (courseEntity.getBweek() > courseEntity1.getEweek() || courseEntity.getEweek() < courseEntity1.getBweek()) {
                break;
            }
            String[] selcoursetime = courseEntity1.getCoursetime().split(",");
            String[] coursetime = courseEntity.getCoursetime().split(",");
            for (String st : selcoursetime) {
                for (String ct : coursetime) {
                    if (st.equals(ct)) {
                        //System.out.println("冲突世界"+st+" "+ct);
                        return false;
                    }
                }
            }
        }
        courseEntity.setLnum(courseEntity.getLnum()+1);
        currcourseDAO.updateCourse(courseEntity);
        return true;
    }

}
